package com.hooper.handler;

import com.hooper.common.Result;
import com.hooper.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author Tim Hooper
 * @version 1.0
 * @time 2023/03/29/13:47
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(BusinessException.class)
    public Result<Object> businessExceptionHandler(BusinessException e) {
        log.error("业务异常:{}\n 详细信息: {}", e.getMessage(), e.getDescription());
        return Result.fail().message(e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public Result<Object> exceptionHandler(Exception e) {
        log.error("系统异常:{}", e.getMessage());
        e.printStackTrace();
        return Result.fail().message(e.getLocalizedMessage() + "\t" + e.getMessage());
    }
}
